﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
using System.Collections.Generic;

namespace HZ.DrugTrade.Web.Admin.BidProdManage
{
    public partial class CustomGoodsAddToHospital : BasePage
    {
        /// <summary>
        /// 
        /// </summary>
        private string CustomsId = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                this.CustomsId = Request.QueryString["ID"];
            }

            if (!Page.IsPostBack)
            {
                //绑定下拉框
                BindSortList();

                //修改状态下 绑定数据
                BindData();
            }
        }

        /// <summary>
        /// 绑定产品分类下拉框
        /// </summary>
        private void BindSortList()
        {
            IBLL_Sort ibbs = Factory.IBLL_Sort();
            IList<Sort> Lsort = ibbs.GetAllSort(null);

            this.dplSort.DataTextField = "SortName";
            this.dplSort.DataValueField = "SortID";
            this.dplSort.DataSource = Lsort;
            this.dplSort.DataBind();

            ListItem item = new ListItem();
            item.Text = "==请选择产品分类==";
            item.Value = Constants.ALL_ITEM_VALUE;
            dplSort.Items.Insert(0, item);
        }

        /// <summary>
        /// 绑定页面数据，仅限修改状态时
        /// </summary>
        private void BindData()
        {
            if (this.CustomsId == string.Empty)
            {
                PageError("产品信息不存在！", ReturnUrl);
            }
            else
            {
                this.tbAdd.Visible = true;

                IBLL_CustomGoods cstCtrl = Factory.BLL_CustomGoods();
                CustomGoods model = cstCtrl.GetCustomGoodsByID(CustomsId);
                if (model != null)
                {
                    if (model.AuditStatus == 3) //3 表示审核已通过
                    {
                        this.txtGoodsName.Text = model.GoodsName;
                        this.txtMaterial.Text = model.Material;
                        this.txtOutlookc.Text = model.Outlookc;
                        this.txtUnit.Text = model.Unit;
                        this.txtCompanyName.Text = model.CompanyName;
                        if (this.dplSort.Items.FindByValue(model.SortID) != null)
                        {
                            this.dplSort.Items.FindByValue(model.SortID).Selected = true;
                        }
                        if (this.dplLevel.Items.FindByValue(model.QualityLevel) != null)
                        {
                            this.dplLevel.Items.FindByValue(model.QualityLevel).Selected = true;
                        }
                        this.txtLimitPrice.Text = model.LimitPrice.Value.ToString("F"); ;
                        this.txtNote.Text = model.Note;

                        this.txtNotes.Text = model.AuditNote;
                        this.txtNotesMS.Text = model.AuditNoteMS;

                        this.txtStatus.Text = FormatAuditStatus(model.AuditStatus);
                        this.txtStatusMS.Text = FormatAuditStatus(model.AuditStatusMS);
                        this.hidHospitalIDs.Value = model.AllowHospitalIDS;

                        if (this.hidHospitalIDs.Value.Trim().Length > 4)
                        {

                            IBLL_Hospital IBH = Factory.BLL_Hospital();
                            int total = 0;
                            Model.Query.Hospital hos = new Model.Query.Hospital();
                            hos.Append = "hospitalid in ('" + this.hidHospitalIDs.Value.Trim().Replace("、", "','") + "')";
                            hos.SortFields = " hospitalid asc";
                            IList<Hospital> lst = IBH.GetHospitalByWhere(hos, 1, 0, out total);

                            if (lst.Count > 0)
                            {
                                foreach (Hospital item in lst)
                                {
                                    this.txtHospitals.Text += item.HospitalName + "；";
                                }
                            }
                        }
                       
                    }
                    else
                    {
                        PageError("产品未审核通过，无法修改！", ReturnUrl);
                    }
                }
                else
                {
                    PageError("产品信息不存在！", ReturnUrl);
                }
                //
                this.btnSave.Text = "修改";
            }

        }
        /// <summary>
        /// 格式化审核状态
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected string FormatAuditStatus(object obj)
        {
            string status = "未知";
            if (obj != null)
            {
                switch (Convert.ToInt16(obj))
                {
                    case 1: status = "未审核";
                        break;
                    case 2: status = "需修改";
                        break;
                    case 3: status = "已通过";
                        break;
                    case 4: status = "已拒绝";
                        break;
                }
            }
            return status;
        }
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strIDs = this.hidHospitalIDs.Value.ToUpper();
            if (this.hidHospitalIDs.Value.Contains(this.hdnHospitalID.Value))
            {
            }
            else
            {
                string[] str = this.hdnHospitalID.Value.Split('、');
                StringBuilder strId = new StringBuilder();
                for (int i = 0; i < str.Length; i++)
                {
                    if (strIDs.IndexOf(str[i].ToUpper())<0)
                    {
                        strId.Append(str[i].ToUpper()+"、");
                    }
                }
                strIDs += "" + strId.ToString();
            }
                CustomGoods model = new CustomGoods()
                {
                    ID = Convert.ToInt32(this.CustomsId),
                    AllowHospitalIDS = strIDs
                };
                IBLL_CustomGoods cstCtrl = Factory.BLL_CustomGoods();
                int result = cstCtrl.UpdateCustomGoods(model);
                {
                    if (result == 1)
                    {
                        PageInfo("操作成功。", "CustomGoodsCheckedList.aspx");
                    }
                    else
                    {
                        PageError("操作失败", "");
                    }
                }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("CustomGoodsCheckedList.aspx");
        }
    }
}
